package org.example.preparelessons.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginInterceptor implements HandlerInterceptor
{
    /**
     * 目标方法执行前
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        Object loginUser = request.getSession().getAttribute("loginUser");
        if (loginUser==null)
    {
        //未登录，返回登录页面
        request.setAttribute("msg","您没有权限进行此操作，请先登陆！");
        //request.getRequestDispatcher()是请求转发，前后页面共享一个request ; 这个是在服 务端运行的，对浏览器来说是透明的。
        request.getRequestDispatcher("/index.html").forward(request,response);
        return false;
    }
    else {
        //放行
         return true;
    }
    }
        /**
         *目标方法执行后
         * @param request
         * @param response
         * @param handler
         * @param modelAndView
         * @throws Exception
         */

         @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
         {
        System.out.println("LoginInterceptor.postHandle 执行： "+modelAndView);
    }
        /**
         * 页面渲染后
         * @param request
         * @param response
         * @param handler
         * @param ex
         * @throws Exception
         */

    @Override
        public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("LoginInterceptor.afterCompletion 执行： "+ex);
    }
    }
